<?php

	error_reporting(0);
	session_start();
	error_reporting(1);
	
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<?php include "../../browser_detect.php"; ?>

<html>
<head>
	<link rel="stylesheet" type="text/css" href="gallery.css" />
</head>
<body style="text-align: left;">

<?php

	$site_root = "../../";
	require_once ($site_root . "common/PinSQL.obj");
	$pinSQL = new PinSQL();
	
	$itemID = $_GET['itemnum']; // this is actually the ID field of the item
	
	$type = $_GET['type'];
	$mode = $_GET['mode']; // NULL or 'popup'

	require_once "Gallery_new.obj";
	include "setupGallery.php"; //creates a $gallery object which we can use for table names and other specifics
	// read contents of table into $result variable
	$query = "SELECT * FROM $gallery->dataTable WHERE id='$itemID'";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);	
		
	// get the following fields from this row (with row index $i):
	$name = $row['name'];
	$artist = $row['artist'];
	$filename = $row['filename'];
	$totalstars = $row['totalstars'];
	$totalratings = $row['totalratings'];
	
	$result_comments = mysql_query("SELECT * FROM $gallery->commentsTable WHERE itemid='$itemID' ORDER BY commentdate DESC") or die(mysql_error());
	
	// get number of rows that $result2 returned, into $num2
	$numcomments = 0;
	$numcomments = mysql_numrows($result_comments);
	
	mysql_close();
     
?>

<img src="img/logo.png" />
<div class="mediatitle"><?php echo $name; ?></div>
<div class="mediaartist"><a href="/members/frame.php?page=profile&user=<?php echo $artist; ?>" target="_top"><?php echo $artist; ?></a></div>
<div class="gallerytype"><?php echo $gallery->pageTitle; ?></div>

<div class="horizontal_separation"></div>

<?php

	$gallery->SetCurrentItem($itemID);
	$gallery->RenderMedia($filename);

?>

<div class="horizontal_separation"></div>

<div class="right_rectangle">
	<img src="img/arrow_pink.gif" />
	Rate this <?php echo $gallery->Identify(); ?>
	<span style="position:relative; left:18px; top:2px;" id="stararea">
<?php
	for ($i=1; $i<=5; $i++)
	{
		echo "<img src=\"img/gauge_empty.png\" width=\"14\" height=\"11\" id=\"star$i\" onclick=\"SetStars($i);\" onmouseover=\"StarHover($i);\" id=\"star$i\" />";
	}
?>
<img src="img/gauge_end.png" />
		<span id="rating_result" class="paleheader_sm"></span>
	</span>
	<br />
	<span class="paleheader" style="position: relative; left: 18px;">Current rating</span>
	<span style="position:relative; left:35px; top:2px;" id="stararea2">

<?php
	// show appropriate number of stars
	if ($totalratings > 0) // this is so it doesn't divide by zero if there are no ratings
	{
		$averagestars = round($totalstars / $totalratings / 2, 0);
		echo "<img src=\"img/gauge_$averagestars.png\" />";
	} else {
	  // the media has not been rated
	}
    

?>
	</span>

	<div class="ratings_numratings" id="ratingsarea">
	   <?php echo $totalratings . ' ratings'; ?>
	</div>
</div>

<div class="horizontal_separation"></div>

<div class="right_rectangle">
      <img src="/img/arrow_green.gif" />
      <a href="<?php echo "/members/userData/$artist/$gallery->containerPath/$filename"; ?>">Download this <?php echo $gallery->Identify(); ?></a>
      <br />
      <img src="/img/arrow_orange.gif" />
      <a href="javascript:showHideId('search')">Suggest this <?php echo $gallery->Identify(); ?></a>
      
      <div id="search" style="display: none;"><blockquote>
      	<div class="searchbar">
			Search users: <img src="/img/user_incoming_tag.png" />
			<input class="inputField" type="text" size="16" id="searchfield" onkeyup="Search();" value=""/>				
		</div>
		
		<input type="hidden" id="touser" />
	
		<div id="searchresults" class="small">
		</div></blockquote>
      </div>
</div>

<div class="horizontal_separation"></div>

<div class="right_rectangle">
      <img src="img/arrow_yellow.gif" />
      <a href="javascript:ShowHideCommentBox()">Write a comment</a>
    
      <div id="commentdiv" style="display: none;">
<?php

if ( isset($_SESSION['username']) )
{
// logged in markup goes here
	?>
           <span id="comment_status" style="display: none; font-size: 9pt;">Please wait...<img src="/img/rotating_arrow.gif" style="position: relative; top: 4px;"></span>
			<div class="horizontal_separation"></div>
           
			<textarea name="comment" id="comment" rows=3 cols=50></textarea>
			
			<div class="horizontal_separation"></div>
			<a href="javascript:PostComment();">> Post Comment</a>
<?php

} else {
// NOT logged in markup goes here
	?>
			<p class="paleheader" />You must be logged in to post comments.
			<p /><img src="img/arrow_pink.gif" /><a href="/frame.php?page=badLogin.php" target="screen">Click here to log in</a>
	<?php
}
	?>
      </div>
</div>

<div class="horizontal_separation"></div>
		       
<div class="right_rectangle">
	<div id="comments"><?php include("gen_comments.php"); ?></div>
</div>

<?php if ($_SESSION['level'] == "admin") { ?>
<div class="horizontal_separation"></div>

<div class="right_rectangle">
	<span class="paleheader" />Administrative tasks</span>
	<span class="waittext" id="action_status"></span>
	<div style="position: relative; left: 20px;"
		<img src="/img/arrow_white.gif" /> <a href="javascript:DoAdminAction('clearratings');">Clear all ratings</a>
		<br /><img src="/img/arrow_white.gif" /> <a href="javascript:DoAdminAction('erasecomments');">Erase all comments</a>
		<br /><img src="/img/arrow_red.gif" /> <a href="javascript:DoAdminAction('deletereference');">Delete reference to this <?php echo $gallery->Identify(); ?></a>
		<br /><img src="/img/arrow_red.gif" /> <a href="javascript:DoAdminAction('deletesong');">Delete <?php echo $gallery->Identify(); ?></a>
	</div>
</div>
<?php } ?>

<div class="horizontal_separation"></div>
<div class="horizontal_separation"></div>

</body>

<script type="text/javascript">

	function Search()
	{
		searchField = document.getElementById('searchfield');

		<?php include "../ajax_init.php" ?>
          
		request.open("POST", '/members/do_action.php?actionname=search', true);
		
		request.onreadystatechange = function()
		{
			if(request.readyState == 4)
			{
				var responseText = request.responseText;
				document.getElementById('searchresults').innerHTML = responseText;
			}
		}

		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.send('mode=suggestions&query=' + searchField.value); 

     }

	function showHideId(id)
	{
		destBlock = document.getElementById(id);
          
	    if (destBlock.style.display == 'block')
	    {
	         destBlock.style.display = 'none';
	    } else {
	         destBlock.style.display = 'block';
	    }
    }

	function StarHover(numStars)
	{
		for (var i=1; i<=5; i++)
		{
			if (i<=numStars)
			{
				document.getElementById('star' + i).src = "img/gauge_white.png";
			} else {
				document.getElementById('star' + i).src = "img/gauge_empty.png";
			}
		}
	}

	function SetStars(numStars)
	{

          // create the XMLHttpRequest object
          var request;
          try { request = new XMLHttpRequest(); }
          catch(e)
          {
               try { request = new ActiveXObject("Msxml2.XMLHTTP"); }
               catch(e)
               {
                    try { request = new ActiveXObject("Microsoft.XMLHTTP"); }
                    catch(e)
                    {
                         alert("Your browser will not support this webpage.");
                         return false;
                    }
               }
          }
          
          request.open("POST", 'add_rating.php', true);
          
          request.onreadystatechange = function()
          {
               if(request.readyState == 4)
               {
	               	var responseArray = request.responseText.split(",");

                    var starHTML = "";
	                starHTML += '<img src="img/gauge_' + responseArray[0] + '.png">' + "\n";
	               	
                    document.getElementById('stararea').innerHTML = starHTML;
                    
                    var average = responseArray[3];
                    
                    starHTML = "";
                    starHTML += '<img src="img/gauge_' + responseArray[3] + '.png">' + "\n";
	               
	               document.getElementById('stararea2').innerHTML = starHTML;
                   document.getElementById('ratingsarea').innerHTML = responseArray[1] + " ratings";
                   
<?php if ($mode != "popup") { ?>
                   parent.Update();
<?php } ?>
                    
               }
          }

		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.send('type=<?php echo $type; ?>&itemid=<?php echo $itemID; ?>&rating=' + numStars + '&visitor=anonymous'); 
		
		document.getElementById('rating_result').innerHTML = "Please wait...";

	}

	function ShowHideCommentBox()
	{
          commentBlock = document.getElementById('commentdiv');
          
          if (commentBlock.style.display == 'block')
          {
               commentBlock.style.display = 'none';
          } else {
               commentBlock.style.display = 'block';
          }
	}
	
	function DeleteComment(commentId)
	{
          // create the XMLHttpRequest object
          var request;
          try { request = new XMLHttpRequest(); }
          catch(e)
          {
               try { request = new ActiveXObject("Msxml2.XMLHTTP"); }
               catch(e)
               {
                    try { request = new ActiveXObject("Microsoft.XMLHTTP"); }
                    catch(e)
                    {
                         alert("Your browser will not support this webpage.");
                         return false;
                    }
               }
          }
          
          request.open("POST", 'delete_comment.php', true);
          
          request.onreadystatechange = function()
          {
               if(request.readyState == 4)
               {
                    var responseText = request.responseText;
                    document.getElementById('comments').innerHTML = responseText;
                    
<?php if ($mode != "popup") { ?>
                   parent.Update();
<?php } ?>
               }
          }

		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.send('type=<?php echo $type; ?>&itemid=<?php echo $itemID; ?>&commentid=' + commentId); 
 	}
 	
	function DoAdminAction(actionName)
	{
          // create the XMLHttpRequest object
          var request;
          try { request = new XMLHttpRequest(); }
          catch(e)
          {
               try { request = new ActiveXObject("Msxml2.XMLHTTP"); }
               catch(e)
               {
                    try { request = new ActiveXObject("Microsoft.XMLHTTP"); }
                    catch(e)
                    {
                         alert("Your browser will not support this webpage.");
                         return false;
                    }
               }
          }
          
          request.open("GET", 'do_action.php?type=<?php echo $type; ?>&itemid=<?php echo $itemID; ?>&actionname=' + actionName, true);
          
          request.onreadystatechange = function()
          {
               if(request.readyState == 4)
               {
	               	var responseText = request.responseText;
                    document.getElementById('action_status').innerHTML = "<br>" + responseText;
                    
                    if ( (actionName == "clearratings") || (actionName == "erasecomments") )
                    {
<?php if ($mode != "popup") { ?>
                   		parent.Update();
<?php } ?>
	                }
               }
          }

		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.send(null); 
		
		document.getElementById('action_status').innerHTML = "<br>Please wait...";
     }	
	
	
	function PostComment()
	{
          statusBlock = document.getElementById('comment_status');
          statusBlock.style.display = 'block';

          // create the XMLHttpRequest object
          var request;
          try { request = new XMLHttpRequest(); }
          catch(e)
          {
               try { request = new ActiveXObject("Msxml2.XMLHTTP"); }
               catch(e)
               {
                    try { request = new ActiveXObject("Microsoft.XMLHTTP"); }
                    catch(e)
                    {
                         alert("Your browser will not support this webpage.");
                         return false;
                    }
               }
          }
          
          request.open("POST", 'post_comment.php', true);
          
          request.onreadystatechange = function()
          {
               if(request.readyState == 4)
               {
                    var responseText = request.responseText;
                    statusBlock.style.display = 'none';
                    commentBlock.style.display = 'none';
                    document.getElementById('comments').innerHTML = responseText;
                    
<?php if ($mode != "popup") { ?>
                   parent.Update();
<?php } ?>
               }
          }

		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.send('type=<?php echo $type; ?>&itemid=<?php echo $itemID; ?>&comment=' + document.getElementById('comment').value); 
     }
          
</script>
</html>